﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraEditors.Controls;

namespace QuanLyThuNgan
{
    public partial class Frm_ThuDtl_Delete : DevExpress.XtraEditors.XtraForm
    {
        public Frm_ThuDtl_Delete()
        {
            InitializeComponent();
        }

        public static string sIDSearch = "";
        public static string sTenValueSearch = "";
        string sTenTam, sColTam;
        int iIDNguoiXoa = frmLogin.nd.IDNguoiDung;

        ThuDTL_DeleteCTL thuDeleteCtl = new ThuDTL_DeleteCTL();
        ThuDTL_DeleteDTO thuDeleteDto = new ThuDTL_DeleteDTO();
        NguoiDungCTL nguoiDungCtl = new NguoiDungCTL();

        private void Frm_ThuDtl_Delete_Load(object sender, EventArgs e)
        {
            dateNgayThu.DateTime = Uit.it_Date.CurrentDate2();
            txtNgayXoa.Text = Uit.it_Date.CurrentDate();

            //load gridlookupedit danh muc ly do xoa-----------------------
            DataTable dtld = thuDeleteCtl.m_SelectLyDoXoa();
            ClassHelp.m_gridLookUpEdit(dtld, grdLyDoXoa, "TenLyDoXoaCT", "IDLyDoXoaCT");

            DataTable dtNguoiDung = nguoiDungCtl.TrangThaiCaThuNgan_Nhap_XacNhan(iIDNguoiXoa);
            if (dtNguoiDung.Rows.Count == 0 || dtNguoiDung.Rows[0]["loaiid"].ToString() == "0")
            {
                btnXoa.Enabled = false;
                XtraMessageBox.Show("Người dùng chỉ được phép xem", "Thông báo");
            }

            m_ReadOnlyValue();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (txtSobnTim.Text != "")
            {
                thuDeleteDto.SSobnTim = txtSobnTim.Text;
                thuDeleteDto.SNgayTim = Uit.it_Parse.ToDateTime(dateNgayThu.EditValue).ToString("yyyy-MM-dd");
                DataTable dtload = thuDeleteCtl.m_SelectThuDTLDelete(thuDeleteDto);
                if (dtload.Rows.Count > 0)
                {
                    txtSobn.Text = dtload.Rows[0]["sobn"].ToString();
                    txtSohs.Text = dtload.Rows[0]["sohs"].ToString();
                    txtHoTen.Text = dtload.Rows[0]["HoTen"].ToString();
                    txtGioiTinh.Text = dtload.Rows[0]["GioiTinh"].ToString();
                    txtNamSinh.Text = dtload.Rows[0]["NamSinh"].ToString();
                    txtSoNhapVien.Text = dtload.Rows[0]["sonhapvien"].ToString();
                    txtNgayNop.Text = dtload.Rows[0]["ngaynop"].ToString();
                    txtSoTien.Text = dtload.Rows[0]["sotien"].ToString();
                    txtTenMucPhi.Text = dtload.Rows[0]["TenMucPhi"].ToString();
                    txtTenThuNgan.Text = dtload.Rows[0]["HoTenNguoiNhap"].ToString();
                    txtTenNoiThu.Text = dtload.Rows[0]["TenDonVi"].ToString();
                    txtTenCa.Text = dtload.Rows[0]["TenCaLamViec"].ToString();

                }
                else
                {
                    XtraMessageBox.Show("Không có thông tin thu tiền", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

       
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            
                if (m_checkXoa() == true)
                {
                    if (txtSobn.Text != "")
                    {
                        thuDeleteDto.SSobnTim = txtSobnTim.Text;
                        thuDeleteDto.SNgayTim = Uit.it_Parse.ToDateTime(dateNgayThu.EditValue).ToString("yyyy-MM-dd");
                        DataTable dtload = thuDeleteCtl.m_SelectThuDTLDelete(thuDeleteDto);
                        foreach (DataRow dr in dtload.Rows)
                        {
                            if (dtload.Rows.Count >0)
                            {
                                thuDeleteDto.Ssobn = dr["sobn"].ToString();
                                thuDeleteDto.Ssohs = dr["sohs"].ToString();
                                thuDeleteDto.Ssonhapvien = dr["sonhapvien"].ToString();
                                thuDeleteDto.Istt = Uit.it_Parse.ToInteger(dr["stt"].ToString());
                                thuDeleteDto.Sngaynop = Uit.it_Parse.ToDateTime(dr["ngaynop"]).ToString("yyyy-MM-dd");
                                thuDeleteDto.Fsotien = Uit.it_Parse.ToInteger(dr["sotien"].ToString());
                                thuDeleteDto.Ifeeid = Uit.it_Parse.ToInteger(dr["feeid"].ToString());
                                thuDeleteDto.Snoivao = dr["noivao"].ToString();
                                thuDeleteDto.ISttThuNgan = Uit.it_Parse.ToInteger(dr["SttThuNgan"].ToString());
                                thuDeleteDto.IIDNguoiNhap = Uit.it_Parse.ToInteger(dr["IDNguoiNhap"].ToString());
                                thuDeleteDto.IIDXacNhan = Uit.it_Parse.ToInteger(dr["IDXacNhan"].ToString());
                                thuDeleteDto.IIDcaThuNgan = Uit.it_Parse.ToInteger(dr["IDcaThuNgan"].ToString());
                            }
                            else
                            {
                                XtraMessageBox.Show("Không có thông tin thu tiền chi tiết", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

                            }

                        }

                        thuDeleteDto.SNgayXoa = Uit.it_Parse.ToDateTime(txtNgayXoa.Text).ToString("yyyy-MM-dd");
                        thuDeleteDto.IIDLyDoXoaCT = Uit.it_Parse.ToInteger(grdLyDoXoa.EditValue.ToString());
                        thuDeleteDto.IIDNguoiXoa = iIDNguoiXoa;
                        thuDeleteDto.SGhiChuXoa = txtGhiChu.Text; 
                        //luu thong tin can xoa
                        thuDeleteCtl.m_InsertThuDTLDelete(thuDeleteDto);
                        thuDeleteCtl.m_DeleteThuDTL(thuDeleteDto);
                        XtraMessageBox.Show("Xóa thành công", "Thông báo");
                        m_NullValue();
                    }
                }
                else
                {
                    XtraMessageBox.Show("Xóa không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

        }

        #region "CheckCondition"
        //set kiem tra truoc thi thuc hien xoa--------------------------------------------------------------------------
        bool m_checkXoa()
        {
            bool bCheckValue = true;
            if (Uit.it_Parse.ToString(grdLyDoXoa.EditValue) == "-1" || Uit.it_Parse.ToString(grdLyDoXoa.EditValue) == "")
            {
                dxErrorProvider1.SetError(grdLyDoXoa, "Phải nhập lý do xóa");
                bCheckValue = false;
            }
            else
            {
                if (Uit.it_Parse.ToString(grdLyDoXoa.EditValue) != "-1" || Uit.it_Parse.ToString(grdLyDoXoa.EditValue) != "")
                {
                    dxErrorProvider1.ClearErrors();
                }

            }
            return bCheckValue;
        }

        //set null value----------
        void m_NullValue()
        {
            txtSobn.Text = "";
            txtSohs.Text = "";
            txtHoTen.Text = "";
            txtGioiTinh.Text = "";
            txtNamSinh.Text = "";
            txtSoNhapVien.Text = "";
            txtNgayNop.Text = "";
            txtSoTien.Text = "";
            txtTenMucPhi.Text = "";
            txtTenThuNgan.Text = "";
            txtTenNoiThu.Text = "";
            txtTenCa.Text = "";
            grdLyDoXoa.EditValue = -1;
            txtGhiChu.Text = "";
        }

        //search gia tri tren girdlookupEdit---------------------
        private void grdLyDoXoa_Enter(object sender, EventArgs e)
        {
            SendKeys.Send("{F4}");
        }

        private void gridLookUpEdit1View_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            sTenTam = e.Value.ToString();
            sColTam = e.Column.FieldName;
        }

        private void gridLookUpEdit1View_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = grdLyDoXoa.Properties.View;
            view.ActiveFilter.Add(view.Columns[sColTam],
              new ColumnFilterInfo("[" + sColTam + "] Like '%" + sTenTam + "%'", ""));
        }

        //set readonly value---------------------
        void m_ReadOnlyValue()
        {
            txtSobn.Properties.ReadOnly = true;
            txtSohs.Properties.ReadOnly = true;
            txtHoTen.Properties.ReadOnly = true;
            txtGioiTinh.Properties.ReadOnly = true;
            txtNamSinh.Properties.ReadOnly = true;
            txtSoNhapVien.Properties.ReadOnly = true;
            txtNgayNop.Properties.ReadOnly = true;
            txtSoTien.Properties.ReadOnly = true;
            txtTenMucPhi.Properties.ReadOnly = true;
            txtTenThuNgan.Properties.ReadOnly = true;
            txtTenNoiThu.Properties.ReadOnly = true;
            txtTenCa.Properties.ReadOnly = true;
        }
        #endregion
    }
}